package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CCustomerLabel;
import com.ruoyi.system.domain.CrmUser;
import com.ruoyi.system.service.ICCustomerLabelService;
import com.ruoyi.system.service.ICrmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CrmLabelMapper;
import com.ruoyi.system.domain.CrmLabel;
import com.ruoyi.system.service.ICrmLabelService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-05
 */
@Service
public class CrmLabelServiceImpl implements ICrmLabelService 
{
    @Autowired
    private CrmLabelMapper crmLabelMapper;

    @Autowired
    private ICCustomerLabelService  cCustomerLabelServiceImpl;

    @Autowired
    private ICrmUserService crmCustomerServiceImpl;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public CrmLabel selectCrmLabelById(Long id)
    {
        return crmLabelMapper.selectCrmLabelById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param crmLabel 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CrmLabel> selectCrmLabelList(CrmLabel crmLabel)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        crmLabel.setCreateUser(userId);
        return crmLabelMapper.selectCrmLabelList(crmLabel);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param crmLabel 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCrmLabel(CrmLabel crmLabel)
    {
        crmLabel.setCreateTime(DateUtils.getNowDate());
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        crmLabel.setCreateUser(userId);
        return crmLabelMapper.insertCrmLabel(crmLabel);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param crmLabel 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCrmLabel(CrmLabel crmLabel)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        crmLabel.setCreateUser(userId);
        return crmLabelMapper.updateCrmLabel(crmLabel);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCrmLabelByIds(Long[] ids)
    {
        return crmLabelMapper.deleteCrmLabelByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCrmLabelById(Long id)
    {

        CCustomerLabel param=new CCustomerLabel();
        param.setLabelId(id);
        List<CCustomerLabel> cCustomerLabels = cCustomerLabelServiceImpl.selectCCustomerLabelList(param);
        List<Long> collect = cCustomerLabels.stream().map(CCustomerLabel::getId).collect(Collectors.toList());
        //删除中间表关系
        if(collect != null && collect.size() > 0){
            Long [] delParam=new Long[collect.size()];
            collect.toArray(delParam);
            cCustomerLabelServiceImpl.deleteCCustomerLabelByIds(delParam);
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //删除客户表内的标签
        CrmUser paramCus=new CrmUser();
        paramCus.setCreateUser(userId);
        List<CrmUser> crmCustomers = crmCustomerServiceImpl.selectCrmCustomerList(paramCus);
        for (CrmUser crmCustomer : crmCustomers
             ) {
            String cusLalg = crmCustomer.getCusLalg();
            String[] split = cusLalg.split(",");
            List<String> strings = Arrays.asList(split);
            StringBuffer sbf=new StringBuffer("");
            for (String lId :strings
                 ) {
                if(!lId.equals(id.toString())){
                    sbf.append(lId).append(",");
                }
            }
            String labS = sbf.toString();
            labS=labS.substring(0,labS.lastIndexOf(","));
            crmCustomer.setCusLabel(labS);
            crmCustomerServiceImpl.updateCrmCustomer(crmCustomer);
        }
        return crmLabelMapper.deleteCrmLabelById(id);
    }

    @Override
    public int deleteCrmLabelByIds(String labelIds) {
        String[] splitLabelIds = labelIds.split(",");
        List<String> ids = Arrays.asList(splitLabelIds);
        for (String idsStr :  ids
             ) {
            Long id= null;
            try {
                id = Long.parseLong(idsStr);
            } catch (NumberFormatException e) {
            }
            if(id != null){
                CCustomerLabel param=new CCustomerLabel();
                param.setLabelId(id);
                //中间表删除关联数据
                List<CCustomerLabel> cCustomerLabels = cCustomerLabelServiceImpl.selectCCustomerLabelList(param);
                if(cCustomerLabels != null && cCustomerLabels.size() > 0){
                    List<Long> collect = cCustomerLabels.stream().map(CCustomerLabel::getId).collect(Collectors.toList());
                    //删除中间表关系
                    if(collect != null && collect.size() > 0){
                        Long [] delParam=new Long[collect.size()];
                        collect.toArray(delParam);
                        cCustomerLabelServiceImpl.deleteCCustomerLabelByIds(delParam);
                    }
                    Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
                    //删除客户表内的标签
                    CrmUser paramCus=new CrmUser();
                    paramCus.setCreateUser(userId);
                    List<CrmUser> crmCustomers = crmCustomerServiceImpl.selectCrmCustomerList(paramCus);
                    for (CrmUser crmCustomer : crmCustomers
                    ) {
                        String cusLalg = crmCustomer.getCusLabel();
                        String[] split = cusLalg.split(",");
                        List<String> strings = Arrays.asList(split);
                        StringBuffer sbf=new StringBuffer("");
                        for (String lId :strings
                        ) {
                            if(!lId.equals(id.toString())){
                                sbf.append(lId).append(",");
                            }
                        }
                        String labS = sbf.toString();
                        if(labS.contains(",")){
                            labS=labS.substring(0,labS.lastIndexOf(","));
                        }
                        crmCustomer.setCusLabel(labS);
                        crmCustomerServiceImpl.updateCrmCustomer(crmCustomer);
                    }
                }
            }
            crmLabelMapper.deleteCrmLabelById(id);
        }
        return 0;
    }
}
